<script setup lang="ts">
import { ref } from 'vue'

const inputRef = ref<HTMLInputElement | null>(null)

const handlePasteClick = () => {
  if (inputRef.value) {
    // 聚焦输入框
    inputRef.value.focus()
    
    // 模拟Ctrl+V按键事件
    const pasteEvent = new KeyboardEvent('keydown', {
      key: 'v',
      code: 'KeyV',
      ctrlKey: true,
      bubbles: true,
      cancelable: true
    })
    
    // 触发事件
    inputRef.value.dispatchEvent(pasteEvent)
  }
}
</script>

<template>
  <div class="paste-input-container">
    <button class="paste-button" @click="handlePasteClick">
      点击粘贴
    </button>
    <input
      ref="inputRef"
      type="text"
      class="paste-input"
      placeholder="点击按钮后自动粘贴"
    />
  </div>
</template>

<style scoped>
.paste-input-container {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 20px 0;
}

.paste-button {
  padding: 8px 16px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.paste-button:hover {
  background-color: #66b1ff;
}

.paste-input {
  padding: 8px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  outline: none;
  transition: border-color 0.3s;
  width: 200px;
}

.paste-input:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}
</style> 